package name.zxw.datastructures.recursion;

import java.util.Arrays;

/**
 * @author ZhangXinWang
 * @date 2023/5/15 23:48
 */
public class EightQueen {

    int max = 8;
    int[] array = new int[max];

    int count = 0;

    int judgeCount = 0;

    public static void main(String[] args) {
        EightQueen eightQueen = new EightQueen();
        eightQueen.check(0);
        System.out.println("eightQueen.count = " + eightQueen.count);
        System.out.println("eightQueen.judgeCount = " + eightQueen.judgeCount);
    }


    private void check(int n) {
        if (n == max) {
            count++;
            System.out.println(Arrays.toString(array));
            return;
        }
        for (int i = 0; i < max; i++) {
            array[n] = i;
            if (judge(n)) {
                check(n + 1);
            }
        }
    }

    //查看当我们放置第n个皇后,检测是否与已经放置的皇后冲突
    private boolean judge(int n) {
        judgeCount++;
        for (int i = 0; i < n; i++) {
            //array[i] == array[n] 表示同一列
            //Math.abs(n-i) == Math.abs(array[n]-array[i]) 表示同一斜线,
            //行数的差值的绝对值 等于 列数差值的绝对值 表示这两个在同一斜线上(好巧妙的转换)
            if (array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {
                return false;
            }
        }
        return true;
    }


}
